﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//
using PolyToPoly_Subdivision.Geometry;
//
namespace PolyToPoly_Subdivision
{
    public partial class Form1 : Form
    {
        //
        public int count_of_chunks=0;
        Polygon nPoly = new Polygon();
        bool isPaint = true;
        Pen myPen = new Pen(Color.Red);
        Graphics formGraphics;
        //
        new List<Polygon> polygonsCache = new List<Polygon>();
        //
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //
           formGraphics = this.CreateGraphics();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            
            //
            /*
            Pen myPen;
            myPen = new Pen(Color.Red);
            Graphics formGraphics = this.CreateGraphics(); 
            List<Polygon> tmpPoly = new List<Polygon>();
            tmpPoly.Add(nPoly);
            Subdivide(formGraphics, myPen, tmpPoly, 0);
            
            myPen.Dispose();
            formGraphics.Dispose();
             */
        }
        //
        private void Subdivide(Graphics graph, Pen pen, List<Polygon> polys, int counter)
        {
            List<Polygon> newPolys = new List<Polygon>();
            //
            if (counter <= 1)
            {
                foreach (Polygon poly in polys)
                {
                    foreach (Polygon _poly in poly.subdivide(12, 0))
                    {
                        graph.DrawPolygon(pen, _poly.GetVerticlesAsPoints());
                        newPolys.Add(_poly);
                        count_of_chunks++;
                    }
                }
                counter++;
                Subdivide(graph, pen, newPolys, counter);
            }
            else
            {
                polygonsCache = polys;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (isPaint)
            {

                //nPoly.AddVertex(Form1.MousePosition.X, Form1.MousePosition.Y);
                nPoly.AddVertex(MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y);

                if (nPoly.GetVerticlesAsPoints().Length > 2)
                {
                    formGraphics.DrawPolygon(myPen, nPoly.GetVerticlesAsPoints());
                }
                else
                {
                    foreach (PointF p in nPoly.GetVerticlesAsPoints())
                    {
                        formGraphics.DrawEllipse(myPen, new Rectangle(new Point((int)p.X,(int)p.Y), new Size(3,3)));
                    }
                }
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            polygonsCache.Add(nPoly);
            Subdivide(formGraphics, myPen, polygonsCache , 0);
            polygonsCache.Remove(nPoly);
        }
    }
}
